package com.example.firstassignment;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyCountries extends Activity {
	
    private Activity main_activity;
    private ListView listView;
    private MyAdapter adapter;
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycountries);
		
		main_activity = this;
		
        listView = (ListView)findViewById(R.id.countryList);
        adapter = new MyAdapter(this,R.layout.mycountries_list_item);
        listView.setAdapter(adapter);
        
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.action_menu, menu);
		return true;
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.add_country: 
        	//System.out.println("Select option works");
        	
        	/* Start new Activity CountryVisited */
        	Intent intent = new Intent(main_activity, CountryVisited.class);
        	//System.out.println(CountryVisited.class);
        	main_activity.startActivityForResult(intent,0);
            return true;
        
        default:
            return super.onOptionsItemSelected(item);
        } 
    }
    /*
     * Adapter to populate list with colors and integers 
     */
    class MyAdapter extends ArrayAdapter<Object> {
    	
    	public MyAdapter(Context context, int textViewResourceId) {
    		super(context,textViewResourceId);
    	}
    	
    	@Override   // Called when updating the ListView
    	public View getView(int position, View convertView, ViewGroup parent) {
    		/* Reuse super handling ==> A TextView from R.layout.list_item */
    		TextView tv = (TextView) super.getView(position,convertView,parent); 
    		/* Find corresponding entry */
    		Object obj = getItem(position);
    		
    		//System.out.println("Object: "+(String)obj); 
    		if(obj instanceof String){
    			tv.setText((String)obj);
    		}
    		
    		return tv;		
    	}
    }
    /** Called when the activity receives a results. */
    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent result) {
    	if (resultCode == RESULT_OK) {
    		String name = result.getStringExtra("result");
    		//System.out.println("Result: "+name);
    		adapter.add(new String(name));
    		listView.setAdapter(adapter);
    	}
    }
}
